package com.b511.swpu.cementbackend.util;

import com.b511.swpu.cementbackend.entity.SysUser;
import com.b511.swpu.cementbackend.repository.SysUserRepository;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class UserUtil {

    private final SysUserRepository sysUserRepository;

    public UserUtil(SysUserRepository sysUserRepository) {
        this.sysUserRepository = sysUserRepository;
    }

    public SysUser getCurrentUser() {
        return sysUserRepository.findByToken(getToken());
    }

    private static String getToken() {
        return (String) SecurityContextHolder.getContext().getAuthentication().getCredentials();
    }
}
